<?php
namespace app\event\controller;

class Wechat
{

    public function active()
    {
        import('org.wechat');
        $wechat = new \wechat();
        if (isset($_GET["wechat"]) && isset($_GET["signature"]) && isset($_GET["echostr"]) && isset($_GET["timestamp"]) && isset($_GET["nonce"])) {
            $wechat->checkAuth();
        }else{
            $checkid = intval($_GET["wechat"]);
            $access_token = $wechat->getAccessToken($checkid);
            $xmlStr = file_get_contents("php://input");
            $xml=xmlArray($xmlStr);
            $this->reply($wechat,$checkid,$access_token,$xml); 
        }
    }

    private function autoreplay($autoreplay,$xml,$keywords = null,$wechat,$access_token)
    {
        if ($keywords != null) {
            foreach ($autoreplay as $key => $value) {
                if ($value["autoreply"] == 1) {
                    //文字回复
                    if (strpos($value["keywords"],$keywords) !==  false) {
                        $send["ToUserName"] = $xml["FromUserName"];
                        $send["FromUserName"] = $xml["ToUserName"];
                        $send["CreateTime"] = $xml["CreateTime"];
                        $send["MsgType"] = "text";
                        $send["Content"] = $value["description"];
                        $str = mkXml($send);
                        echo $str;
                        exit();
                    }
                    
                }else if ($value["autoreply"] == 2) {
                    //文字回复
                    if (strpos($value["keywords"],$keywords) !==  false) {
                        // $media['media']='@'.fullUrl($value["img_url"]);
                        $media['media']='@/var/local/www/xcx/public/pub'.$value["img_url"];
                        // $media['media']='@/var/local/www/xcx/public/static/file/wechat/wechat.jpg';
                        $media_id = $wechat->addMedia($access_token,"image",$media);
                        $str = '<xml><ToUserName><![CDATA['.$xml["FromUserName"].']]></ToUserName><FromUserName><![CDATA['.$xml["ToUserName"].']]></FromUserName><CreateTime>'.$xml["CreateTime"].'</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA['.$media_id.']]></MediaId></Image></xml>';
                        echo $str;
                        exit();
                    }
                    
                }else if ($value["autoreply"] == 3) {
                    //文字回复
                    if (strpos($value["keywords"],$keywords) !==  false) {
                        //转盘红包
                        $t = $value["title"];//title
                        $d = $value["description"];//description
                        $p = $value["img_url"];//picurl
                        $u = $value["article_url"];//url
                        $str = '<xml><ToUserName><![CDATA['.$xml["FromUserName"].']]></ToUserName><FromUserName><![CDATA['.$xml["ToUserName"].']]></FromUserName><CreateTime>'.$xml["CreateTime"].'</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA['.$t.']]></Title><Description><![CDATA['.$d.']]></Description><PicUrl><![CDATA['.$p.']]></PicUrl><Url><![CDATA['.$u.']]></Url></item></Articles></xml>';
                        echo $str;
                        exit();
                        exit();
                    }
                    
                }
            }
        }else{
            foreach ($autoreplay as $key => $value) {
                if ($value["autoreply"] == 1) {
                    //文字回复
                    $send["ToUserName"] = $xml["FromUserName"];
                    $send["FromUserName"] = $xml["ToUserName"];
                    $send["CreateTime"] = $xml["CreateTime"];
                    $send["MsgType"] = "text";
                    $send["Content"] = $value["description"];
                    $str = mkXml($send);
                    echo $str;
                    exit();
                    
                }else if ($value["autoreply"] == 2) {
                    //文字回复
                    // $media['media']='@'.fullUrl($value["img_url"]);
                    $media['media']='@/var/local/www/xcx/public/pub'.$value["img_url"];
                    // $media['media']='@/var/local/www/xcx/public/static/file/wechat/wechat.jpg';
                    $media_id = $wechat->addMedia($access_token,"image",$media);
                    $str = '<xml><ToUserName><![CDATA['.$xml["FromUserName"].']]></ToUserName><FromUserName><![CDATA['.$xml["ToUserName"].']]></FromUserName><CreateTime>'.$xml["CreateTime"].'</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA['.$media_id.']]></MediaId></Image></xml>';
                    echo $str;
                    exit();
                    
                }else if ($value["autoreply"] == 3) {
                    //图文回复
                    $t = $value["title"];//title
                    $d = $value["description"];//description
                    $p = $value["img_url"];//picurl
                    $u = $value["article_url"];//url
                    $str = '<xml><ToUserName><![CDATA['.$xml["FromUserName"].']]></ToUserName><FromUserName><![CDATA['.$xml["ToUserName"].']]></FromUserName><CreateTime>'.$xml["CreateTime"].'</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA['.$t.']]></Title><Description><![CDATA['.$d.']]></Description><PicUrl><![CDATA['.$p.']]></PicUrl><Url><![CDATA['.$u.']]></Url></item></Articles></xml>';
                    echo $str;

                    exit();
                    
                }
            }
        }
        
    }

    //自动回复
    private function reply($wechat,$checkid,$access_token,$xml)
    {
        //关键词
        if ($xml["MsgType"] == "text") {
            //消息回复
            $where1["event"] = 1;
            $where1["wechat_id"] = $checkid;
            $autoreplay = db("wechat_autoreplay")->where($where1)->select();
            $this->autoreplay($autoreplay,$xml,$xml['Content'],$wechat,$access_token);
            //自动回复
            $where1["event"] = 2;
            $where1["wechat_id"] = $checkid;
            $autoreplay = db("wechat_autoreplay")->where($where1)->select();
            $this->autoreplay($autoreplay,$xml,null,$wechat,$access_token);

        }else if ($xml["Event"] == "subscribe" && $xml["MsgType"] == "event") {
            //自动回复
            $where1["event"] = 3;
            $where1["wechat_id"] = $checkid;
            $autoreplay = db("wechat_autoreplay")->where($where1)->select();
            $this->autoreplay($autoreplay,$xml,null,$wechat,$access_token);

        }else if ($xml["Event"] == "CLICK" && $xml["MsgType"] == "event") {
            //菜单事件
            $where1["event"] = 4;
            $where1["wechat_id"] = $checkid;
            $autoreplay = db("wechat_autoreplay")->where($where1)->select();
            $this->autoreplay($autoreplay,$xml,$xml["EventKey"],$wechat,$access_token);
            
        }else{
            echo "success";
        }
    }

    public function filterEmoji($str){
        $str = $str = preg_replace_callback('/./u',function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },$str);
        return trim($str);
    }
    
}